package com.goodflower.goodflower.creat_flower;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.goodflower.goodflower.R;
import com.goodflower.goodflower.info.Info;
import com.goodflower.goodflower.util.HttpUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class Creat_flower_one extends AppCompatActivity {
    private EditText creat_flower_id;//传感器编号
    private Button btn_creat_flower_one_next;//下一页编号
    //  private String[] id_list = new String[50];
    private List<String> id_list = new ArrayList<>();
    private String mac_mac;
    private String mac_flower_name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.creat_flower_one);
        creat_flower_id = (EditText) findViewById(R.id.creat_flower_id);
        btn_creat_flower_one_next = (Button) findViewById(R.id.btn_creat_flower_one_next);
        final Intent intent = getIntent();
        mac_mac = intent.getStringExtra("mac_mac");
        mac_flower_name = intent.getStringExtra("mac_flower_name");
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpUtil.sendOkHttpRequest("http://"+ Info.server_ip+"/huahua/idselect.php?mac=" + mac_mac, new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {

                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String res = response.body().string();
                        String[] id_split = res.split("\\|");
                        int id = 0;
                        int fid = 1;
                        for (String myid : id_split) {
                            if (myid.equals("#")) {
                                //  id_list[id] = Integer.toString(fid)+" 号传感器";
                                id_list.add(Integer.toString(fid) + " 号传感器");
                                id++;
                            }
                            fid++;
                        }
                    }
                });
            }
        }).start();

        creat_flower_id.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                final AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
                builder.setTitle("请选择传感器编号");
                if (id_list.size() == 0) {
                    builder.setMessage("您已经没有多余的传感器了,立即购买享受更加丰富的智能体验!");
                    builder.setNegativeButton("不了", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });
                    builder.setPositiveButton("立即购买", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {

                        }
                    });
                    builder.show();
                } else {
                    final String[] strings = new String[id_list.size()];
                    for (int i = 0, j = id_list.size(); i < j; i++) {
                        strings[i] = id_list.get(i);
                    }

                    builder.setItems(strings, new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            // Toast.makeText(v.getContext(),"您选择了"+strings[which],Toast.LENGTH_LONG).show();
                            creat_flower_id.setText(strings[which].substring(0, 1));
                        }
                    });
                    builder.show();
                }

            }
        });
        btn_creat_flower_one_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (id_list.contains(creat_flower_id.getText().toString() + " 号传感器")) {
                    Intent creat_flower_one_intent = new Intent(Creat_flower_one.this, Creat_flower_two.class);
                    creat_flower_one_intent.putExtra("flower_mac", mac_mac);
                    creat_flower_one_intent.putExtra("flower_id", creat_flower_id.getText().toString());
                    creat_flower_one_intent.putExtra("mac_flower_name",mac_flower_name);
                    startActivity(creat_flower_one_intent);
                } else {
                    Toast.makeText(v.getContext(), "亲,您给的传感器编号不在允许的范围内\n请从提供的选项中选o～", Toast.LENGTH_LONG).show();
                }

            }
        });
    }
}
